
let level = [
    {
        call: "用药安全专家",
        img: "static/img/level1.png"
    },
    {
        call: "家庭小医生",
        img: "static/img/level4.png"
    },
    {
        call: "药师小学徒",
        img: "static/img/level3.png"
    },
    {
        call: "绝命毒师",
        img: "static/img/level2.png"
    },
]
import { wxShare } from '@/utils'
import $ from 'jquery'
import html2canvas from "html2canvas"
import commonFn from "@/utils/commonFn/index"
export default {
    name: "",
    data() {
        return {
            imgSrc: "",
            userName: "",
            rightNum: 5,
            level: level,
            addName: true,
            callName: '用药安全专家',
            levelImg: '',
            date: '',
            time: '',
            showMask: false,
            scale: 0,
            isIOS: false,
            isBuild: false,
            req: {}
        };
    },
    methods: {
        buildImg() {
            var dom = document.getElementById("html-img");
            html2canvas(dom, {
                useCORS: true,
                allowTaint: false
            }).then(canvas => {
                this.imgSrc = canvas.toDataURL("image/jpeg");
                this.addName = false
                setTimeout(() => { this.$vux.loading.hide(); }, 1000)
            });

        },
        build() {
            const rightNum = this.rightNum
            if (rightNum == 10) {
                this.callName = this.level[0].call
                this.levelImg = this.level[0].img
            } else if (rightNum <= 9 && rightNum > 7) {
                this.callName = this.level[1].call
                this.levelImg = this.level[1].img
            } else if (rightNum <= 7 && rightNum > 4) {
                this.callName = this.level[2].call
                this.levelImg = this.level[2].img
            } else if (rightNum <= 4 && rightNum >= 0) {
                this.callName = this.level[3].call
                this.levelImg = this.level[3].img
            }
            let allTime = commonFn.formatTime(new Date())
            this.date = allTime.split(' ')[0]
            this.time = allTime.split(' ')[1]
        },
        percentage(rightNum) {
            switch (Number(rightNum)) {
                case 10:
                    return '90'
                    break;
                case 9:
                    return '80'
                    break;
                case 8:
                    return '70'
                    break;
                case 7:
                    return '60'
                    break;
                case 6:
                    return '50'
                    break;
                case 5:
                    return '40'
                    break;
                case 4:
                    return '30'
                    break;
                case 3:
                    return '20'
                    break;
                case 2:
                    return '10'
                    break;
                case 1:
                    return '5'
                    break;
                case 0:
                    return '0'
                    break;
                default:
                    break;
            }
        },
        hideMask() {
            this.showMask = false
        },
        downLoad() {
            this.downHandle()
            
        },
        again() {
            this.$router.push('/')
        },
        share() {
            wxShare({
                title: "这些常用药99%的人都用错了，测一测你都用对了么？",
                text: `我答对${this.rightNum}道题，打败了全${this.scale}%的用户！`,
                href: 'http://app.allclinic.cn/h5/wx/startActivity/#/?source_type=3',
                imgUrl:
                    "https://app.allclinic.cn/h5/wx/scan_drug/static/img/logo.jpg",
                    appMessageSuccFn: () => {
                        this.shareHandle()
                    },
                    timeLineSuccFn: () => {
                        this.shareHandle()
                    }
            });
        },
        loadImg(){
            var img = document.querySelector('#img');
            var newImg = new Image();
            newImg.src = 'http://app.allclinic.cn/h5/wx/startActivity/' + this.levelImg
            newImg.onload = () => { // 图片加载成功后把地址给原来的img
                img.src = newImg.src
                this.buildImg()
            }
        },
        downHandle(){
            this.$$api_global_question({
                data: {
                    ...this.req,
                    download_flag: 1
                },
                fn: () => {
                    window.location.href = 'https://app.allclinic.cn/h5/#/clients/download'
                },
                succFn(){

                }
            })
        },
        shareHandle(){
            this.$$api_global_question({
                data: {
                    ...this.req,
                    transmit_flag: 1
                },
                noLoading: true
            })
        }
    },
    filters: {
        result(newValue) {
            if (newValue == 10) {
                return '恭喜你全部答对了！'
            } else {
                return `恭喜你答对${newValue}题`
            }
        },
    },
    mounted() {
        this.$vux.loading.show({
            text: "数据生成..."
        });
        this.req = JSON.parse(window.localStorage.getItem('reqData'))
        $('.html-img').css({ height: window.screen.height })
        this.rightNum = this.$route.query.allRightNum
        // alert(localStorage.getItem("user"))
        this.userName = JSON.parse(localStorage.getItem("user")).nickname
        this.scale = this.percentage(this.rightNum)
        this.share()
        this.build()
        this.$nextTick(res => {
            this.loadImg()
        })
    }
};
